<?php


function manage_plant_list_page()
{
    @$output = drupal_render(drupal_get_form('plant_list_form'));
    return $output;
}


function plant_list_form($form, &$form_state)
{
    $form['plant'] = array(
        '#prefix' => '<div class="plant-fuzzy-search">',
        '#suffix' => '</div>',
        '#type' => 'textfield',
        '#default_value' => !empty($form_state['values']['fuzzy_plant']) ? $form_state['values']['fuzzy_plant'] : '',
        '#attributes' => array('placeholder' => '作物名称'),
        '#validated' => TRUE,
    );
    $form['plant_category'] = _insert_plant_category_selection_form($form, $form_state);
    $table['#markup'] = data_list_view_page($form, $form_state);
    ajax_submit_form($form, $form_state, $table, '');
    return $form;
}

function data_list_view_page($form = '', &$form_state = '')
{
    $output = '';
    //表头
    $header = array(
        array('data' => '选择', 'class' => ['essential no']),
        array('data' => '作物名称', 'class' => ['essential plant_name'] ),
        array('data' => '类别', 'class' => ['essential name'],'field' => 'plant_category_id'),
        array('data' => '操作', 'class' => ['essential more']),
    );
    $query = db_select('plant', 'P')->extend('PagerDefault')->extend('TableSort');
    $query->join('plant_category', 'C', "C.plant_category_id=P.plant_category_id");
    $query->fields('P');
    $query->addField('C','plant_category_name');
    //模糊条件筛选
    if (!empty($form_state['values']['plant'])) {//模糊地址查询条件
        $query->condition('P.plant_name', '%' . db_like($form_state['values']['plant']) . '%', 'LIKE');
    }
    if (!empty($form_state['values']['plant_category'])) {//模糊地址查询条件
        $query->condition('P.plant_category_id',$form_state['values']['plant_category']);
    }
    if (empty($form_state['values']) || empty($form_state['values']['order'])) {
        $query->orderBy('P.timestamp', 'DESC');
    }

    $query->limit(variable_get("list_page_item_number", 5));

    $plantInfo = $query->orderByHeader($header)->execute();

    $rows = array();
    //表内容
    $n = 1;
    foreach ($plantInfo as $row) {
        $rows[] = array(
            array('data' => '<input type="checkbox" class="listViewTableCheck" value ="' . $row->plant_id . '"/>' . $n++, 'class' => ['essential no']),
            array('data' => $row->plant_name, 'class' => ['essential plant_name']),
            array('data' => $row->plant_category_name, 'class' => ['essential plant_category_name']),
            array('data' => '<a data-trigger="modal" href="' . base_path() . 'manage_plant/edit/' . $row->plant_id . '" data-title="修改" class="btn btn-mini">修改</a>', 'class' => 'essential edit'),
        );
    }
    $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('没有记录'), 'attributes' => array('class' => array('table table-bordered table-striped table-hover enhanced')))); //表格
    /*	 * *按钮** */
    if (sizeof($rows) > 0) {
        $output .= '<a href="javascript:void(0)" id="listViewTableSelectAll" data-toggle="modal" class="btn btn-mini">全选</a>';
        $output .= '<a href="javascript:void(0)" id="listViewTableDeleteSelected" data-toggle="modal" class="btn btn-mini">删除所选</a>';
    }
    $output .= theme('pager');
    return $output;
}

function manage_plant_delete()
{
    ajax_table_delete('plant', 'plant_id');

}